﻿using System;
using System.Windows;

namespace Red.Controls.TimeLine.Core
{
    public class TimeRuler : Red.Controls.Ruler
    {
        #region Dependency properties    
            
        public TimeLineControler TimeLineControler
        {
            get { return (TimeLineControler)GetValue( TimeLineControlerProperty ); }
            set { SetValue( TimeLineControlerProperty, value ); }
        }

        public static readonly DependencyProperty TimeLineControlerProperty =
            DependencyProperty.Register( "TimeLineControler", typeof( TimeLineControler ), typeof( TimeRuler ),
                new PropertyMetadata( null, OnTimeControlerChange ) );

        private static void OnTimeControlerChange( DependencyObject d, DependencyPropertyChangedEventArgs e )
        {
            TimeRuler ruler = (TimeRuler)d;

            TimeLineControler oldControler = (TimeLineControler)e.OldValue;
            TimeLineControler newControler = (TimeLineControler)e.NewValue;

            if ( oldControler != null )
            {
                oldControler.StateChanged -= ruler.TimeRulerTimeControlerStateChanged;
            }

            if ( newControler != null )
            {
                newControler.StateChanged += ruler.TimeRulerTimeControlerStateChanged;               
                // reset ruler's settings
                ruler.TimeRulerTimeControlerStateChanged( newControler, null );
            }
        }

        private void TimeRulerTimeControlerStateChanged( object sender, EventArgs e )
        {
            TimeLineControler timeControler = (TimeLineControler)sender;

            StartValue = timeControler.StartTime;

            MaxValue = timeControler.MaxTimeLineValue;
            MinValue = timeControler.MinTimeLineValue;

            MajorIntervalUnit = timeControler.MajorIntervalUnit;
            MajorIntervalSizePx = timeControler.MajorIntervalSizePx;
            MajorIntervalDivision = timeControler.MajorIntervalDivision;

            MinorIntervalUnit = timeControler.MinorIntervalUnit;
            MinorIntervalSizePx = timeControler.MinorIntervalSizePx;
            SetRulerState();
        }

        #endregion
    }
}
